//
//  ListenSongssViewController.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/25.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "ListenSongssViewController.h"
#import "IFly.h"
#import "CustomerNetworking.h"
#import "Songer.h"
#import "ListenSongsFindSongerViewController.h"
@interface ListenSongssViewController ()<IFlySTOPDelegate>

@property (nonatomic, assign) BOOL ListenTrue;
@property (nonatomic, retain) CATransition *transition;
@property (nonatomic, retain) IFly *iflyMake;
@property (nonatomic, retain) UIButton *ListenButton;
@property (nonatomic, copy) NSString *listenString;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIImageView *picImage;
@end

@implementation ListenSongssViewController

- (void)dealloc
{
    [_transition release];
    [_array release];
    [_ListenButton release];
    [_listenString release];
    [_picImage release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.array = [NSMutableArray array];
    self.iflyMake = [IFly shareManager];
    self.iflyMake.Stopdelegate = self;
    self.transition = [CATransition animation];
    //设置动画时长
    [self.transition setDuration:2];
    //设置动画的重复次数
    [self.transition setRepeatCount:100];
    //设置动画效果
    self.transition.type = @"rippleEffect";
    //self.transition.subtype = kCATransitionFromLeft;
    
    self.ListenTrue = YES;
    self.ListenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.ListenButton.frame = CGRectMake(0, 50, 375, 375);
    //[self.ListenButton setImage:[UIImage imageNamed:@"listening"] forState:UIControlStateNormal];
    [self.ListenButton addTarget:self action:@selector(listenAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_ListenButton];
    
    
    self.picImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 355, 355)];
    self.picImage.image = [UIImage imageNamed:@"listening"];
    [self.view addSubview:_picImage];
}

- (void)listenAction:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        if (_ListenTrue == YES) {
            self.listenString = @"";
            [self.picImage.layer addAnimation:_transition forKey:@"transition"];
            //[sender setImage:[UIImage imageNamed:@"listenStop"] forState:UIControlStateNormal];
            self.picImage.image = [UIImage imageNamed:@"listenStop"];
            [self.iflyMake recognizerBlock:^(NSString *str) {
                self.listenString = [NSString stringWithFormat:@"%@%@",self.listenString,str];
            }];
            _ListenTrue = NO;
        }else{
            //[sender setImage:[UIImage imageNamed:@"listening"] forState:UIControlStateNormal];
            self.picImage.image = [UIImage imageNamed:@"listening"];
            [self.picImage.layer removeAnimationForKey:@"transition"];
            [_iflyMake stopReconizer];
            _ListenTrue = YES;
        }
    }else{
        if (_ListenTrue == NO) {
            //[sender setImage:[UIImage imageNamed:@"listening"] forState:UIControlStateNormal];
            self.picImage.image = [UIImage imageNamed:@"listening"];
            [self.picImage.layer removeAnimationForKey:@"transition"];
            [_iflyMake stopReconizer];
            _ListenTrue = YES;
        }else{
            self.listenString = @"";
            //[sender setImage:[UIImage imageNamed:@"listenStop"] forState:UIControlStateNormal];
            self.picImage.image = [UIImage imageNamed:@"listenStop"];
            [self.picImage.layer addAnimation:_transition forKey:@"transition"];
            [self.iflyMake recognizerBlock:^(NSString *str) {
                self.listenString = [NSString stringWithFormat:@"%@%@",self.listenString,str];
            }];
            _ListenTrue = NO;
        }
    }
}

- (void)stopButton
{
    [self.picImage.layer removeAnimationForKey:@"transition"];
    //[self.ListenButton setImage:[UIImage imageNamed:@"listening"] forState:UIControlStateNormal];
    self.picImage.image = [UIImage imageNamed:@"listening"];
    _ListenTrue = YES;
    [self customer];
}

- (void)customer
{
    [_array removeAllObjects];
    NSString *url = [_listenString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.dongting.com/misc/search/song?q=%@&page=1&size=50&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88248&longtitude=121.5398", url] andParameters:nil andBlock:^(id response) {
        NSArray *arr = [response objectForKey:@"data"];
        
        for (NSDictionary *dic in arr)
        {
            Songer *song = [Songer new];
            [song setValuesForKeysWithDictionary:dic];
            if (song.auditionList.count != 0)
            {
                [self.array addObject:song];
            }
            [song release];
        }
        if (self.array.count != 0) {
            ListenSongsFindSongerViewController *listensong = [[ListenSongsFindSongerViewController alloc] init];
            listensong.array = _array;
            [self.navigationController pushViewController:listensong animated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
